package com.haidao.admin.component.web.controller;

import com.haidao.admin.component.entity.ReportRecord;
import com.haidao.admin.component.entity.Task;
import com.haidao.admin.component.params.IdParam;
import com.haidao.admin.component.params.TaskParam;
import com.haidao.admin.component.params.TaskUpdateParam;
import com.haidao.admin.component.service.ReportRecordService;
import com.haidao.admin.component.service.TaskService;
import com.haidao.admin.component.web.job.ReportJob;
import com.haidao.admin.core.result.Resp;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController {

    @Resource
    private TaskService taskService;
    @Resource
    private ReportRecordService reportRecordService;

    /**
     * 任务列表
     */
    @PostMapping("/list")
    public Resp<?> list(@RequestBody TaskParam param) throws Exception {
        Page<Task> tasks = taskService.list(param);
        return Resp.ok(tasks);
    }

    /**
     * 任务创建
     */
    @PostMapping("/create")
    public Resp<?> create(@RequestBody Task param) throws Exception {
        taskService.create(param);
        return Resp.ok();
    }

    /**
     * 任务创建
     */
    @PostMapping("/start")
    public Resp<?> start(@RequestBody Task param) throws Exception {
        Task task = taskService.detail(param.getId());
        Map<String, Object> params = new HashMap<>();
        params.put("code", task.getTaskKey());
        params.put("taskId", task.getId());
        taskService.startNow(String.valueOf(task.getId()), params, ReportJob.class);
        return Resp.ok();
    }

    /**
     * 任务修改
     */
    @PostMapping("/update")
    public Resp<?> update(@RequestBody Task param) throws Exception {
        taskService.update(param);
        return Resp.ok();
    }

    /**
     * 任务修改
     */
    @PostMapping("/batch/update")
    public Resp<?> batchUpdate(@RequestBody TaskUpdateParam param) throws Exception {
        taskService.batchUpdate(param);
        return Resp.ok();
    }

    @PostMapping("/detail")
    public Resp<?> update(@RequestBody IdParam param) throws Exception {
        Task task = taskService.detail(param.getId());

        ReportRecord totalQuery = new ReportRecord();
        totalQuery.setTaskId(task.getId());
        long total = reportRecordService.count(totalQuery);
        task.setReportCount(total);

        ReportRecord failQuery = new ReportRecord();
        failQuery.setTaskId(task.getId());
        failQuery.setFailCount(1);
        long fail = reportRecordService.count(failQuery);
        task.setFailCount(fail);

        ReportRecord successQuery = new ReportRecord();
        successQuery.setTaskId(task.getId());
        successQuery.setSuccessCount(1);
        long success = reportRecordService.count(successQuery);
        task.setSuccessCount(success);
        return Resp.ok(task);
    }
}